using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using NovodexWrapper;

/// <summary>
/// Arrow used to point the target
/// </summary>
/// 
namespace marbletrack
{
    public class PointingArrow
    {
        private VertexPositionNormalTexture[] pointList = new VertexPositionNormalTexture[5];
        private int[] triangleListIndices = new int[] 
        { 
            0, 2, 1,
            1,2,0,
            //position 0 - 5 1st triangle
            0, 3, 2,
            2,3,0,
            //position 6 - 11 2nd triangle
            0,4,3,
            3,4,0,
            //position 12 - 17 3rd triangle
            0, 1, 4,
            4,1,0,
            //position 18 - 23 4th triangle
            1, 2, 4,
            4,2,1,
            2,3,4,
            4,3,2
            //position 24 - 35 square
        };
        private Vector3 squareColourOne = new Vector3(255, 0, 0);
        private Vector3 squareColourTwo = new Vector3(0, 0, 255);
        VertexDeclaration vertexDeclaration;

        private Vector3 position = Vector3.Zero;
        private Vector3 right = Vector3.Zero;
        private Vector3 forward = Vector3.Zero;
        private Vector3 up = Vector3.Zero;
        private Matrix world = Matrix.Identity;
        private Matrix view = Matrix.Identity;

        private float rotationAngle = 0;
        private float RotationAngle
        {
            get
            {
                rotationAngle +=10;
                //stops the angle going to high
                if (rotationAngle > 360)
                {
                    rotationAngle = 0;
                }
                return MathHelper.ToRadians(rotationAngle);
            }
        }

        public PointingArrow()
        {
            pointList.Initialize();
            //Vertex Declaration
            vertexDeclaration = new VertexDeclaration(Game1.Graphics.GraphicsDevice, VertexPositionNormalTexture.VertexElements);

            //setting up arrow
            pointList[0].Position = Vector3.UnitX * Game1.SQUARESIZE * 3;
            pointList[1].Position = -Vector3.UnitZ * Game1.HALFSQUARESIZE + Vector3.UnitY * Game1.HALFSQUARESIZE;
            pointList[2].Position = Vector3.UnitZ * Game1.HALFSQUARESIZE + Vector3.UnitY * Game1.HALFSQUARESIZE;
            pointList[3].Position = Vector3.UnitZ * Game1.HALFSQUARESIZE + -Vector3.UnitY * Game1.HALFSQUARESIZE;
            pointList[4].Position = -Vector3.UnitZ * Game1.HALFSQUARESIZE + -Vector3.UnitY * Game1.HALFSQUARESIZE;
        }

        public void Update()
        {
            squareColourOne.Y = Vector3.Distance(Game1.lastTrackPiece.exitPoint_, Game1.endPiece.modelPosition_)/10000;
            squareColourTwo.Y = Vector3.Distance(Game1.lastTrackPiece.exitPoint_, Game1.endPiece.modelPosition_) / 10000;
        }

        public void Draw()
        {
            position = Game1.lastTrackPiece.exitPoint_ + Vector3.UnitY * 2 * Game1.SQUARESIZE;
            right = Game1.endPiece.modelPosition_ - Game1.lastTrackPiece.exitPoint_;
            forward = Vector3.Cross(right, up);
            up = Vector3.Cross(right, Vector3.UnitY*100);

            right.Normalize();
            forward.Normalize();
            up.Normalize();

            world = Matrix.CreateTranslation(position);
            world.Right = right;
            world.Forward = forward;
            world.Up = up;
            Game1.basicEffect.World = Matrix.CreateRotationX(RotationAngle)*world;

            Game1.Graphics.GraphicsDevice.VertexDeclaration = vertexDeclaration;
            Game1.basicEffect.TextureEnabled = false;
            Game1.basicEffect.View = Game1.view;



            Game1.basicEffect.Projection = Game1.projectionMatrix;
            //Game1.basicEffect.Texture = texture_;

            //draw 1st triangle
            Game1.basicEffect.DiffuseColor = squareColourOne;
            Game1.basicEffect.Begin();
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.TriangleList, pointList, 0, 5, triangleListIndices, 0, 2);
                pass.End();
            }
            Game1.basicEffect.End();

            //draw 3rd triangle
            Game1.basicEffect.Begin();
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.TriangleList, pointList, 0, 5, triangleListIndices, 12, 2);
                pass.End();
            }
            Game1.basicEffect.End();

            //draw 2nd triangle
            Game1.basicEffect.DiffuseColor = squareColourTwo;
            Game1.basicEffect.Begin();
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.TriangleList, pointList, 0, 5, triangleListIndices, 6, 2);
                pass.End();
            }
            Game1.basicEffect.End();

            //draw 4th triangle
            Game1.basicEffect.Begin();
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.TriangleList, pointList, 0, 5, triangleListIndices,18, 2);
                pass.End();
            }
            Game1.basicEffect.End();

            //draw square
            Game1.basicEffect.DiffuseColor = Color.GreenYellow.ToVector3();
            Game1.basicEffect.Begin();
            foreach (EffectPass pass in Game1.basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                Game1.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
                PrimitiveType.TriangleList, pointList, 0, 5, triangleListIndices, 24, 4);
                pass.End();
            }
            Game1.basicEffect.End();

            Game1.basicEffect.TextureEnabled = true;
            Game1.basicEffect.DiffuseColor = Color.White.ToVector3();
            Game1.basicEffect.World = Matrix.Identity;
        }
    }
}